﻿using fuzhu;
using LH.U8API.Bus;
using SqlSuarEntity;
using SqlSugar;
using SqlSugar.Extensions;
using System.Collections.Generic;
using System.Data;
using U8COWeb;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.WMS;
using ZDEntity;
using static XXX.Models.WMS.ScanBarcode;

namespace Yonyou.U8.BO
{
    //扫描条码的临时表保存
    public class ZDY_RDRECORDS_TMPVM : ZDY_RDRECORDS_TMP
    {
        //当前的数据库连接字符串
        public string conStr { get; set; }
        public string cacc { get; set; }
        public string xmlConfig { get; set; }
        public static string cDatabase = "";
      
        public ZDY_RDRECORDS_TMPVM()
        {
            //conStr
            conStr = CanShu.conStr;
            cacc = CanShu.cZT;
            //初始化定义xmlconfig
            xmlConfig = XmlOperation.GetConfigXML();
        }

        public void ClearConstr()
        {
            conStr = "";
            xmlConfig = "";
        }
        /// <summary>
        /// 查询扫描的暂存数据，使用ctype区分类别
        /// </summary>
        /// <param name="ccode">pda 上传入，csourcecode+cliendid，已拼接好</param>
        /// <param name="ctype"></param>
        /// <returns></returns>
        public RetModel2 ST_Rdrecord_Tmp_Get(string ccode, string ctype)
        {
            try
            {
                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {
                    ScanBarcodeVM scan = db.Queryable<ZDY_RDRECORDS_TMP>()
                      .Where(s => s.cType == ctype && s.cSourceCode == ccode)
                      .Select(s => new ScanBarcodeVM
                      {
                          cParentid = s.PARENTID,
                          cWhCode = s.cWhCode,
                          cDepCode = s.cDepCode,
                          cRdCode = s.cRdCode,
                          cWhName = s.cWhName,
                          cType = s.cType,
                          cUserName = s.cMaker,
                          cUserId = s.cUserId,
                          cDefine1 = s.cDefine1,
                          cDefine2 = s.cDefine2,
                          cDefine3 = s.cDefine3,
                          cDefine4 = s.cDefine4


                      }).First();
                    if (scan == null)
                    {
                        return DBService.ReErrObject("没有暂存数据");
                    }


                    scan.barcodelist = db.Queryable<ZDY_RDRECORDS_TMP>()
                       .InnerJoin<Inventory>((s, b) => s.cInvCode == b.cInvCode)
                        .Where((s, b) => s.PARENTID == scan.cParentid)
                        .Select((s, b) => new ScanBarcode.ScanBarcodes
                        {
                            cBarcodeno = s.cBarcodeno,
                            cInvCode = s.cInvCode,
                            cInvName = b.cInvName,
                            cBatch = s.cBatch,
                            cPosCode = s.cPosCode,
                            cPosCodeIn = s.cPosCode2,
                            dMDate = s.dMakeDate,
                            dVDate = s.dVDate,
                            autoid = s.iSourceId,
                            iQuantity = s.iQuantity,
                            cFree1 = s.cFree1,
                            cFree2 = s.cFree2,
                            cFree3 = s.cFree3,
                            cFree4 = s.cFree4
                        })
                        .ToList();



                    scan.itemList = db.Queryable<ZDY_RDRECORDS_TMP>()
                        .InnerJoin<Inventory>((s, b) => s.cInvCode == b.cInvCode)
                        .Where((s, b) => s.PARENTID == scan.cParentid)
                       .Select((s, b) => new ScanBarcode.ScanBarcodes
                       {
                           cBarcodeno = s.cBarcodeno,
                           cInvCode = s.cInvCode,
                           cInvName = b.cInvName,
                           cBatch = s.cBatch,
                           cPosCode = s.cPosCode,
                           cPosCodeIn = s.cPosCode2,
                           autoid = s.iSourceId,
                           iQuantity = s.iQuantity,
                           dMDate = s.dMakeDate,
                           dVDate = s.dVDate,
                           cFree1 = s.cFree1,
                           cFree2 = s.cFree2,
                           cFree3 = s.cFree3,
                           cFree4 = s.cFree4

                       })
                       .ToList();
                    //清除连接字符串
                    scan.ClearConstr();
                    List<ScanBarcodeVM> lst = new List<ScanBarcodeVM>();
                    lst.Add(scan);
                    return DBService.ReOKObject(lst);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }

        }



        /// <summary>
        /// 产出产成品入库单的暂存数据
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public RetModel2 ST_Rdrecord_Tmp_Save(ScanBarcodeVM scanBarcodes)
        {

            if (scanBarcodes.itemList == null)
            {
                return DBService.ReErrObject("没有数据，无法保存");
            }
            if (scanBarcodes.itemList.Count == 0)
            {
                return DBService.ReErrObject("没有数据，无法保存");
            }
            

            try
            {
                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {
                    string sql = "";
                    //查询数据库里已经存在的数据,如果系统里原来有，使用之前的parentid，没有的话，是新的
                    string cParentid = "";

                    string cSourceCode = scanBarcodes.cCode + "/" + scanBarcodes.clientid;
                    List<ZDY_RDRECORDS_TMP> lstori = db.Queryable<ZDY_RDRECORDS_TMP>()
                        .Where(it => it.cSourceCode == cSourceCode && it.cType == scanBarcodes.cType)
                        .ToList();
                    if (lstori.Count > 0)
                    {
                        cParentid = lstori[0].PARENTID;

                    }
                    else
                    {
                        cParentid = System.Guid.NewGuid().ToString();
                    }



                    List<ZDY_RDRECORDS_TMP> lst = new List<ZDY_RDRECORDS_TMP>();
                    for (int i = 0; i < scanBarcodes.barcodelist.Count; i++)
                    {
                        if (!lstori.Exists(t => t.cSourceCode == cSourceCode && t.cType == scanBarcodes.cType && t.cBarcodeno == scanBarcodes.barcodelist[i].cBarcodeno))
                        {

                            ZDY_RDRECORDS_TMP rd = new ZDY_RDRECORDS_TMP();
                            rd.PARENTID = cParentid;
                            rd.cSourceCode = scanBarcodes.cCode + "/" + scanBarcodes.clientid;
                            rd.cInvCode = scanBarcodes.barcodelist[i].cInvCode;
                            rd.cBatch = scanBarcodes.barcodelist[i].cBatch;
                            rd.iQuantity = scanBarcodes.barcodelist[i].iQuantity;
                            rd.cType = scanBarcodes.cType;
                            rd.cMaker = scanBarcodes.cUserName;
                            rd.dDate = scanBarcodes.dDate;
                            rd.cWhCode = scanBarcodes.cWhCode;
                            rd.cWhName = scanBarcodes.cWhName;
                            //货位按表体存储
                            rd.cPosCode = scanBarcodes.barcodelist[i].cPosCode;
                            //rd.cPosName = scanBarcodes.cPosName;
                            rd.cVenCode = scanBarcodes.cVenCode;
                            rd.iSourceId = scanBarcodes.barcodelist[i].autoid;
                            rd.cBarcodeno = scanBarcodes.barcodelist[i].cBarcodeno;
                            rd.dMaketime = DateTime.Now;
                            rd.cRdCode = scanBarcodes.cRdCode;
                            rd.cDefine1 =scanBarcodes.cDefine1;
                            rd.cDefine2 = scanBarcodes.cDefine2;
                            rd.cDefine3 = scanBarcodes.cDefine3;
                            rd.cDefine4 = scanBarcodes.cDefine4;
                            rd.dMakeDate = scanBarcodes.barcodelist[i].dMDate;
                            rd.dVDate = scanBarcodes.barcodelist[i].dVDate;
                            rd.cFree1 = scanBarcodes.barcodelist[i].cFree1;
                            rd.cFree2 = scanBarcodes.barcodelist[i].cFree2;
                            rd.cFree3 = scanBarcodes.barcodelist[i].cFree3;
                            rd.cFree4 = scanBarcodes.barcodelist[i].cFree4;
                            lst.Add(rd);


                        }


                    }
                    if (lst.Count==0)
                    {
                        return DBService.ReErrObject("没有新增数据，无需保存");
                    }
                    var cnt = db.Insertable<ZDY_RDRECORDS_TMP>(lst).ExecuteCommand();
                    if (cnt > 0)
                    {
                        List<ZDY_RDRECORDS_TMP_FenPei> lst2 = new List<ZDY_RDRECORDS_TMP_FenPei>();
                        for (int i = 0; i < scanBarcodes.itemList.Count; i++)
                        {
                            if (!lstori.Exists(t => t.cSourceCode == cSourceCode && t.cType == scanBarcodes.cType && t.cBarcodeno == scanBarcodes.itemList[i].cBarcodeno))
                            {

                                ZDY_RDRECORDS_TMP_FenPei rd2 = new ZDY_RDRECORDS_TMP_FenPei();
                                rd2.PARENTID = cParentid;
                                rd2.cInvCode = scanBarcodes.itemList[i].cInvCode;
                                rd2.cBatch = scanBarcodes.itemList[i].cBatch;
                                rd2.iQuantity = scanBarcodes.itemList[i].iQuantity;
                                rd2.cWhCode = scanBarcodes.cWhCode;
                                rd2.cPosCode = scanBarcodes.itemList[i].cPosCode;
                                rd2.iSourceId = scanBarcodes.itemList[i].autoid;
                                rd2.cBarcodeno = scanBarcodes.itemList[i].cBarcodeno;
                                rd2.dMaketime = DateTime.Now;
                                rd2.dMakeDate = scanBarcodes.itemList[i].dMDate;
                                rd2.dVDate = scanBarcodes.itemList[i].dVDate;
                                rd2.cFree1 = scanBarcodes.itemList[i].cFree1;
                                rd2.cFree2 = scanBarcodes.itemList[i].cFree2;
                                rd2.cFree3 = scanBarcodes.itemList[i].cFree3;
                                rd2.cFree4 = scanBarcodes.itemList[i].cFree4;
                                lst2.Add(rd2);
                            }
                        }
                        var cnt2 = db.Insertable<ZDY_RDRECORDS_TMP_FenPei>(lst2).ExecuteCommand();

                        if (cnt == 0)
                        {
                            return DBService.ReErrObject("未保存成功，请重试");
                        }

                       
                    }
                    return DBService.ReOKObject("保存成功");
                }
               
            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
            
            
        }

        /// <summary>
        /// 产出产成品入库单的暂存数据,使用ctype区分类别
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public RetModel2 ST_Rdrecord_Tmp_Del(string ccode, string ctype)
        {
            try
            {
                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {
                    //删除分配表
                    db.Deleteable<ZDY_RDRECORDS_TMP_FenPei>().Where(p => SqlFunc.Subqueryable<ZDY_RDRECORDS_TMP>()
                    .Where(s => s.PARENTID == p.PARENTID && s.cType == ctype && s.cSourceCode == ccode).Any())
                        .ExecuteCommand();

                    db.Deleteable<ZDY_RDRECORDS_TMP>().Where(s =>  s.cType == ctype && s.cSourceCode == ccode) 
                                          .ExecuteCommand();

                  
                    return DBService.ReOKObject("删除完成");
                }
                
             

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }

        }

        /// <summary>
        /// 删除行暂估数据
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public RetModel2 ST_Rdrecord_Tmp_DelRow(string ccode, string ctype, string cBarcode)
        {
            try
            {


                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {
                    //删除分配表
                    db.Deleteable<ZDY_RDRECORDS_TMP_FenPei>().Where(p => SqlFunc.Subqueryable<ZDY_RDRECORDS_TMP>()
                    .Where(s => s.PARENTID == p.PARENTID && s.cType == ctype && s.cSourceCode == ccode && p.cBarcodeno==cBarcode).Any())
                        .ExecuteCommand();

                    db.Deleteable<ZDY_RDRECORDS_TMP>().Where(s => s.cType == ctype && s.cSourceCode == ccode && s.cBarcodeno ==cBarcode)
                                          .ExecuteCommand();


                    return DBService.ReOKObject("删除完成");
                }


                

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }

        }

    }
}
